<!--
  * 市场报表-订货商品汇总表
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form>
      <a-row :gutter="[8, 10]">
        <a-col :span="5.5">
          <a-form-item label="制单时间" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="from.time" />
          </a-form-item>
        </a-col>
        <a-col :span="3">
          <a-form-item label="商品名称" class="smart-margin-bottom0">
            <a-input v-model:value.trim="from.keyword" placeholder="商品名称" autofocus allowClear />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch" v-privilege="'goods:query'">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetData" v-privilege="'goods:query'">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="onExport" type="primary" v-privilege="'goods:exportGoods'">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="goods_id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading" :row-selection="{ fixed: true, selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'goods_name'">
            <CopyText :text="text">
              <text>{{ text }}</text>
            </CopyText>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>总计</a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell
                ><a-typography-text type="danger">{{ sumData.total_order_num }}</a-typography-text></a-table-summary-cell
              >
              <a-table-summary-cell
                ><a-typography-text type="danger">{{ Number(sumData.total_order_amount).toFixed(2) }}</a-typography-text></a-table-summary-cell
              >
              <a-table-summary-cell
                ><a-typography-text type="danger">{{ sumData.fulfilled_num }}</a-typography-text></a-table-summary-cell
              >
              <a-table-summary-cell
                ><a-typography-text type="danger">{{ Number(sumData.fulfilled_amount).toFixed(2) }}</a-typography-text></a-table-summary-cell
              >
              <a-table-summary-cell
                ><a-typography-text type="danger">{{ sumData.pending_num }}</a-typography-text></a-table-summary-cell
              >
              <a-table-summary-cell
                ><a-typography-text type="danger">{{ Number(sumData.pending_amount).toFixed(2) }}</a-typography-text></a-table-summary-cell
              >
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </a-table>
    </div>

    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="() => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import { marketApi } from '@/api/business/report/market-api'
import TableOperator from '@/components/support/table-operator/index.vue'
import { useQuery } from '@/hooks/web/use-query-hook'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { useResize } from '@/hooks/web/use-resize-hook'
// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()

// ---------------------------- 表格列 ----------------------------

const columns = ref([
  {
    title: '商品名称',
    dataIndex: 'goods_name'
  },
  {
    title: '订货数量',
    dataIndex: 'total_order_num'
  },
  {
    title: '订货金额',
    dataIndex: 'total_order_amount'
  },
  {
    title: '已履约数量',
    dataIndex: 'fulfilled_num'
  },
  {
    title: '已履约金额',
    dataIndex: 'fulfilled_amount'
  },
  {
    title: '未履约数量',
    dataIndex: 'pending_num'
  },

  {
    title: '未履约金额',
    dataIndex: 'pending_amount'
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const from = ref({
  time: DEFAULT_TIME_RANGE.value,
  keyword: ''
})
const sumData = ref({
  fulfilled_amount: 0,
  fulfilled_num: 0,
  order_count: 0,
  pending_amount: 0,
  pending_num: 0,
  total_order_amount: 0,
  total_order_num: 0
})
const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData } = useQuery({
  fetchDataApi: async (params) => {
    const data = {
      ...params,
      time: from.value.time,
      keyword: from.value.keyword
    }
    const res = await marketApi.queryBookList(data)
    sumData.value = res.data.sum
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  queryFormFilter: {},
  queryFormOp: {}
})
onMounted(() => {
  queryData()
})
// 搜索重置
const resetData = () => {
  from.value.time = DEFAULT_TIME_RANGE.value
  from.value.keyword = ''
  resetQuery()
}

// ---------------------------- 批量 ----------------------------

// 选择表格行
const selectedRowKeyList = ref([])

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys
}
// ------------------------------- 导出和导入 ---------------------------------
// 导出
function onExport() {
  const data = {
    time: from.value.time,
    keyword: from.value.keyword
  }
  marketApi.exportBook(data)
}
</script>
